using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Personalplaner.Modules.Personoverview
{
    [ToolboxItem(false)]
    public partial class DiversView : UserControl, IPersonDetail
    {
        List<IPersonRow> row = null;
        DataTable dt = new DataTable("1");
        int prsID;

        public DiversView(int prsid)
        {
            InitializeComponent();

            this.prsID = prsid;

			dateVon.Value = DateTime.Today;
			dateBis.Value = DateTime.Today;

            SetData();
        }

        public void SetData()
        {
			if (diversGrid.Columns.Count > 0)
				diversGrid.Columns.Clear();

			dt = new DataTable("1");

			DDiversGenerator divGenerator = new DDiversGenerator(prsID, dateVon.Value.ToString(), dateBis.Value.ToString());
            row = divGenerator.Row;

            diversGrid = GPersonGridGenerator.CreateGrid(diversGrid, dt, GPersonGridGenerator.GRIDTYPE.Divers, row);
        }

        private void setGridSize()
        {
            if (diversGrid.Rows.Count > 0)
            {
                //Height
                if (this.Size.Height > 100)
                {
                    int height = ((diversGrid.RowCount * diversGrid.Rows[0].Height) + diversGrid.ColumnHeadersHeight) + 19;
                    if (this.Size.Height < (height + 100))
                    {
                        diversGrid.Height = this.Size.Height - 100;
                    }
                    else
                    {
                        diversGrid.Height = height;
                    }
                }

                //Width
                if (this.Size.Width > 120)
                {
                    int width = 0;
                    int i = 0;
                    while (i < diversGrid.ColumnCount)
                    {
                        width += diversGrid.Columns[i].Width;
                        i++;
                    }

                    if (this.Size.Width < (width + 120))
                    {
                        diversGrid.Width = this.Size.Width - 120;
                    }
                    else
                    {
                        diversGrid.Width = width + 19;
                    }
                }
            }
        }

        private void CPersonDetailDivers_SizeChanged(object sender, EventArgs e)
        {
            setGridSize();
        }

		private void dateVon_ValueChanged(object sender, EventArgs e)
		{
			if (dateBis.Value < dateVon.Value)
			{
				dateBis.Value = dateVon.Value;
			}
		}

		private void btnUpdate_Click(object sender, EventArgs e)
		{
			SetData();
		}

        public DataGridView Grid
        {
            get { return diversGrid; }
        }

		public string Title
		{
			get { return "Diverse Anzeigen"; }
		}

		#region IPersonDetail Members


		public void ResetGrid(int year)
		{
			//Do nothing
		}

		#endregion
	}
}
